var tools = {
	getNowDate(){
		// 创建一个 Date 对象
		const today = new Date();
		// 获取年份
		const year = today.getFullYear();
		// 获取月份（注意：月份是从 0 开始的，所以要加 1）
		const month = today.getMonth() + 1;
		// 获取日期
		const day = today.getDate();
		return year+"-"+month+"-"+day;
	},
	async getCode() {
		let code = this.getQueryValue('code') //是否存在code
		if (!code) {
			const corpId = 'ww89944e61e3e02d29'; // 企业微信的企业ID
			const redirectUri =
				encodeURIComponent('https://scrm.zuixiyu.com/mdyjgl'); // 授权回调地址
			const state = 'STATE_STATE'; // 自定义参数，用于防止CSRF攻击
			var huancun = uni.getStorageSync("huancun");
			const authUrl =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corpId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
			window.location.replace(authUrl)
		} else {
			var codeStorage = uni.getStorageSync("code");
			if (code != codeStorage) {
				uni.setStorageSync("code", code);
				this.toLogin(code);
			} else {
				window.location.href = "https://scrm.zuixiyu.com/mdyjgl"
			}
		}
	},
	formatDateString(dateString) {
		// 按 '-' 分割字符串
		const parts = dateString.split('-');
		// 确保分割后的数组长度为 3（年、月、日）
		if (parts.length !== 3) {
			throw new Error('日期格式不正确，请使用 YYYY-MM-DD 格式');
		}
		// 提取年、月、日
		let [year, month, day] = parts;
		// 补零：月份和日期
		month = month.padStart(2, '0'); // 如果月份是 1 位，补零
		day = day.padStart(2, '0'); // 如果日期是 1 位，补零
		// 返回格式化后的字符串
		return `${year}-${month}-${day}`;
	},
	getNewDateTime(){
		// 创建一个 Date 对象
		const now = new Date();
		
		// 获取年、月、日、时、分、秒
		const year = now.getFullYear(); // 年 (四位数)
		const month = now.getMonth() + 1; // 月 (0-11，需要 +1)
		const day = now.getDate(); // 日 (1-31)
		const hours = now.getHours(); // 时 (0-23)
		const minutes = now.getMinutes(); // 分 (0-59)
		const seconds = now.getSeconds(); // 秒 (0-59)
		
		// 格式化输出
		const formattedDate = `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
		const formattedTime = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
		return `${formattedDate} ${formattedTime}`;
	},
	getNowDate() {
		// 创建一个 Date 对象
		const today = new Date();
		// 获取年份
		const year = today.getFullYear();
		// 获取月份（注意：月份是从 0 开始的，所以要加 1）
		const month = today.getMonth() + 1;
		// 获取日期
		const day = today.getDate();
		var info=year + "-" + month + "-" + day;
		return this.formatDateString(info);
	},
	// 获取微信token
	getWxcode() {
		var tokenInfo = uni.getStorageSync("tokenInfo");
		const timestamp = Date.now();
		let code = this.$tools.getQueryValue('code') //是否存在code
		if (tokenInfo.time < timestamp) { //重新获取token
			if (!code) {
				const corpId = 'ww89944e61e3e02d29'; // 企业微信的企业ID
				const redirectUri =
					encodeURIComponent('https://scrm.zuixiyu.com/mdyjgl'); // 授权回调地址
				const state = 'STATE_STATE'; // 自定义参数，用于防止CSRF攻击
				var huancun = uni.getStorageSync("huancun");
				const authUrl =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corpId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
				window.location.replace(authUrl)
			}
			return false;
		} else {
			return true; //
		}
	},

	getQueryValue(name, str) {
		try {
			let url = str || window.location.href
			
			let reg = new RegExp('(^|&|\\?)' + name + '=([^&#]*)(&|#|$)')
			let r = url.match(reg)
			if (r != null) {
				return decodeURIComponent(r[2])
			}
			return ''
		} catch (error) {
			alert(error)
		}
	},


	//解决富文本图片溢出
	formatRichText(html) {
		let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
			match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
			match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
			match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
			return match;
		});
		newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
			match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
				'max-width:100%;');
			return match;
		});
		newContent = newContent.replace(/<br[^>]*\/>/gi, '');
		newContent = newContent.replace(/\<img/gi,
			'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
		return newContent;
	},
	getKaishijieshu() {
		var nowDate = new Date();
		var cloneNowDate = new Date();
		var fullYear = nowDate.getFullYear();
		var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11，代表1-12月
		var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天
		function getFullDate(targetDate) {
			var D, y, m, d;
			if (targetDate) {
				D = new Date(targetDate);
				y = D.getFullYear();
				m = D.getMonth() + 1;
				d = D.getDate();
			} else {
				y = fullYear;
				m = month;
				d = date;
			}
			m = m > 9 ? m : '0' + m;
			d = d > 9 ? d : '0' + d;
			return y + '-' + m + '-' + d;
		};
		var endDate = getFullDate(cloneNowDate.setDate(endOfMonth)); //当月最后一天
		var starDate = getFullDate(cloneNowDate.setDate(1)); //当月第一天
		console.log(starDate)
		console.log(endDate)
	},
	// 验证手机号
	isphone(phone) {
		if (!(/^1[3456789]\d{9}$/.test(phone))) {
			return false;
		}
		return true;
	},
	isEmail(email) {
		if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
			return true;
		else
			return false;
	},
	//获取订单编号
	getOrderNumber() {
		var timestamp = Date.parse(new Date());
		var Range = 100 - 1;
		var Rand = Math.random();
		return timestamp + (1 + Math.round(Rand * Range));
	},
	//获取随机数
	getSuiji() {
		var Range = Max - Min;
		var Rand = Math.random();
		return (Min + Math.round(Rand * Range));
	},
	//轻提示
	showToast(txt) {
		uni.showToast({
			title: txt,
			'icon': 'none'

		})
	},
	//计算多长时间前
	getDateDiff(dateTimeStamp) {
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var halfamonth = day * 15;
		var month = day * 30;
		var year = day * 365;
		var now = new Date().getTime();
		var diffValue = now - dateTimeStamp;
		if (diffValue < 0) {
			return;
		}
		var yearC = diffValue / year;
		var monthC = diffValue / month;
		var weekC = diffValue / (7 * day);
		var dayC = diffValue / day;
		var hourC = diffValue / hour;
		var minC = diffValue / minute;
		if (yearC >= 1) {
			result = "" + parseInt(yearC) + "年前";
		}
		if (monthC >= 1) {
			result = "" + parseInt(monthC) + "月前";
		} else if (weekC >= 1) {
			result = "" + parseInt(weekC) + "周前";
		} else if (dayC >= 1) {
			result = "" + parseInt(dayC) + "天前";
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时前";
		} else if (minC >= 1) {
			result = "" + parseInt(minC) + "分钟前";
		} else
			result = "刚刚";
		return result;
	},
	timestampToTime(timestamp) {
		var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear() + '-';
		var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
		var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
		var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
		return Y + M + D + h + m + s;
	},

	/**判断是否是手机号**/
	isPhoneNumber(tel) {
		var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
		return reg.test(tel);
	}
}

//将时间戳转换成正常时间格式
export default tools;